package com.zhangbaiyan.test3;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * @author zhangbaiyan
 * @date 2024-09-26 00:04
 * @description:
 * @version:
 */
public class send {
    public static void main(String[] args) throws IOException {
        /*
          按照下面的要求实现程序
                UDP发送数据:数据来之于键盘录入,直到输入的数据是886,发送数据结束
                UDP接收数据:因为接收端不知道发送端什么时候停止发送,故采用死循环接收
         */

        //1.创建对象DatagramSocket的对象
        DatagramSocket ds = new DatagramSocket();

        //打包数据
        Scanner sc = new Scanner(System.in);
        DatagramPacket dp = null;
        while (true) {
            System.out.println("请输入您要说的话:");
            String str = sc.nextLine();
            byte[] bytes =str.getBytes();
            InetAddress address= InetAddress.getByName("river");
            int port = 10086;
            dp = new DatagramPacket(bytes, bytes.length, address, port);
            //发送数据
            ds.send(dp);
            if (str.equals("886"))break;
        }


        //释放资源
        ds.close();



    }
}
